package chloroform;

import scaladuct.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * <p/>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Dec 20, 2010
 * Time: 9:15:07 PM
 */
public class ConfigPageInJavaA extends AbstractConfigPage<String> {

    public String id() {
        return "ConfigPageInJavaA";
    }

    public Object getCookie() {
        return new CookieA();
    }

    public ConfigResult<String> consume(Object cookie) {
        final CookieA cookieA = (CookieA) cookie;
        if (cookieA.a == 1)
            return new NewStarter(new Starter<String>() {
                public String newInstance() {
                    return cookieA.toString();
                }

                public void destroy(String instance) {
                }
            });
        else
            return new NextPage<String>(new ConfigPageInJavaB(cookieA));
    }

    public class ConfigPageInJavaB extends AbstractConfigPage<String> {

        private final CookieA a;

        public ConfigPageInJavaB(CookieA a) {
            this.a = a;
        }

        public String id() {
            return "ConfigPageInJavaB";
        }

        public Object getCookie() {
            return new CookieB();
        }

        public ConfigResult<String> consume(final Object cookie) {
            return new NewStarter(new Starter<String>() {
                public String newInstance() {
                    return a.toString() + "," + cookie.toString();
                }

                public void destroy(String instance) {
                }
            });
        }


        private class CookieB {
            @Size(min = 3, max = 10)
            String x;
            float y;

            public String getX() {
                return x;
            }

            public void setX(String x) {
                this.x = x;
            }

            public float getY() {
                return y;
            }

            public void setY(float y) {
                this.y = y;
            }

            @Override
            public String toString() {
                return x + y;
            }
        }
    }

    private static class CookieA {
        @Min(10)
        int a;

        Date d = new Date();

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }

        @Temporal(TemporalType.DATE)
        public Date getD() {
            return d;
        }

        public void setD(Date d) {
            this.d = d;
        }

        @Override
        public String toString() {
            return d.toString() + ":" + a;
        }
    }
}
